//
//  PlayViewController.m
//  CivilianTravel
//
//  Created by xalo on 16/7/14.
//  Copyright © 2016年 许仕杰. All rights reserved.
//

#import "PlayViewController.h"
#import "PlayTableViewCell.h"
#import "ShoppingViewController.h"
#import "SpotModel.h"
#import "HomePageViewController.h"
#import "SpecificModol.h"
#import "CityModel.h"

@interface PlayViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(strong,nonatomic)NSMutableArray *playArray;



@end

@implementation PlayViewController

-(NSMutableArray *)playArray{
    if (!_playArray) {
        _playArray = [NSMutableArray array];
    }
    return _playArray;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;

    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self.view addSubview:self.tableView];

    [self.tableView registerNib:[UINib nibWithNibName:@"PlayTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];

    CityModel *modol = [[CityModel alloc]init];
    
    [self lodingWithCityId:self.IDD bigCityId:modol.ID startpos:@(0)];
   
}

// 请求数据

-(void)lodingWithCityId:(NSNumber *)cityID bigCityId:(NSNumber *)bigcity startpos:(NSNumber *)startpos{


    [RequestManager fetchDataWithUrl:@"http://www.roadqu.com/api/mobile/qunawan/poi/search" parameter:@{@"a_t":@"1468410964000",@"cityid":self.IDD,@"token":@"578624ce19a3f"} method:@"POST" compeleted:^(NSData *data) {

        NSDictionary *BigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        for (NSDictionary *dic in BigDic[@"data"]) {
            SpotModel *desModol = [[SpotModel alloc]init];
            [desModol setValuesForKeysWithDictionary:dic];
            [self.playArray addObject:desModol];
        }
        
    } updateUI:^{

        
        [self.tableView reloadData];

    }];

    
}


// tableview的实现方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.playArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PlayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell lodingWithModol:self.playArray[indexPath.row]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    tableView.estimatedRowHeight = 20;
    tableView.rowHeight = UITableViewAutomaticDimension;
    return tableView.rowHeight;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    
    ShoppingViewController *shopp = [[ShoppingViewController alloc]init];
    
    
    SpotModel *modol = self.playArray[indexPath.row];
    
    shopp.VCid = modol.code;
    
    

    
    [self.navigationController pushViewController:shopp animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
